#ifndef HTTP_CONNECTION_H
#define HTTP_CONNECTION_H
#include <QObject>
#include <QHash>
#include "http_parser.h"
#define  HTTP_TEST //MARK20170927
class QTcpSocket;
class HttpRequest;
class HttpResponse;
typedef QHash<QString, QString> HeaderHash;
class HttpConnection : public QObject
{
    Q_OBJECT
public:
    HttpConnection(QTcpSocket *socket, QObject *parent = 0);
    virtual ~HttpConnection();
    void write(const QByteArray &data);
    void flush();
signals:
    void newRequest(HttpRequest*, HttpResponse*);
    void newArray(QByteArray,QByteArray);//MARK0808
private slots:
    void parseRequest();
    void responseDone();
private:
    static int MessageBegin(http_parser *parser);
    static int Path(http_parser *parser, const char *at, size_t length);
    static int QueryString(http_parser *parser, const char *at, size_t length);
    static int Url(http_parser *parser, const char *at, size_t length);
    static int Fragment(http_parser *parser, const char *at, size_t length);
    static int HeaderField(http_parser *parser, const char *at, size_t length);
    static int HeaderValue(http_parser *parser, const char *at, size_t length);
    static int HeadersComplete(http_parser *parser);
    static int Body(http_parser *parser, const char *at, size_t length);
    static int MessageComplete(http_parser *parser);
#ifdef HTTP_TEST
    void timeOutOrDone(); //MARK20170927 Add
#endif
private:
    QTcpSocket *m_socket;
    http_parser_settings m_parserSettings;
    http_parser *m_parser;
    HttpRequest *m_request;
    HeaderHash m_currentHeaders;
    QString m_currentHeaderField;
    QString m_currentHeaderValue;
#ifdef HTTP_TEST //MARK20170927
    QByteArray mRevArr;
    static QString mPostData;
    static QByteArray mrealdata;
    bool mHaveHead;
    quint32 mContentLen;
    quint32 mContentIndex;
#endif
};
#endif
